﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using eRechnung.Properties;
using ebDoc = eRechnung.Globals;
using System.Diagnostics;


namespace eRechnung
{
    public partial class DsInvoice
    {
        partial class TblInvSkontoDataTable
        {

            public void UpdateSkontoTage(TblInvSkontoRow myRow)
            {
                decimal tage = -1;
                DateTime Datum;
                TimeSpan ts = new TimeSpan();
                //                string strReDatum = eRechnung.Globals.ThisDocument.CC_InvoiceDate.Text.ToString();

                Datum = eRechnung.Globals.ThisDocument.RechnungsdatumInTemplate;

                if (!(myRow.IsSpBisDatumNull()))  // Bis Datum ist vorhanden
                {
                    tage = myRow.SpBisDatum.Subtract(Datum).Days;
                }
                myRow.SpTage = tage;
                Debug.WriteLine("Skonto Row changed:" +
                                   myRow.SpTage.ToString() + ";" +
                                   myRow.SpBisDatum.ToShortDateString() + ";" +
                                   myRow.SpSkontoProz.ToString("0.00%") + ";" +
                                   myRow.spBasisBetrag.ToString("0.00") + ";" +
                                   myRow.spSkontoBetrag.ToString("0.00"));
            }

            public void UpdateSkontoDatum(TblInvSkontoRow Row)
            {
                DateTime Datum;
                //                string strReDatum = eRechnung.Globals.ThisDocument.CC_InvoiceDate.Text.ToString();

                Datum = eRechnung.Globals.ThisDocument.Rechnungsdatum;

                DateTime newSpDate = Datum.AddDays(Convert.ToDouble(Row.SpTage));
                Row.SpBisDatum = newSpDate;
                Debug.WriteLine("Skonto Row changed:" +
                                   Row.SpTage.ToString() + ";" +
                                   Row.SpBisDatum.ToShortDateString().ToString() + ";" +
                                   Row.SpSkontoProz.ToString("0.00%") + ";" +
                                   Row.spBasisBetrag.ToString("0.00") + ";" +
                                   Row.spSkontoBetrag.ToString("0.00"));
            }

            public bool IsSkontoDateValid(DateTime dueDate)
            {
                int cnt = this.Count(row => row.SpBisDatum > dueDate);
                return cnt == 0;
            }
        }

        partial class TblInvDetailsDataTable
        {
            public bool IsDetailsTableValid(out string errorText)
            {
                string errText = "";
#if ebInterface4Bund                
                if (this.Rows.Cast<TblInvDetailsRow>().Any(row => !Globals.ThisDocument.IsBestellBezugValid(row.SpBestellBezug, out errText)))
                {
                    errorText = errText;
                    return false;
                }  
#endif
                errorText = "";
                return true;
            }
        }

        partial class TblMwStSummenDataTable
        {
        }

        public void LoadMwStDefaults()
        {
            this.TblMWST.Clear();
            Assembly Asm = Assembly.GetExecutingAssembly();
            Stream St = Asm.GetManifestResourceStream("eRechnung.MwStDefaults.xml");
            StreamReader ResRd = new StreamReader(St);
            DataSet myDS = new DataSet();
            myDS.ReadXml(St);
            DataTable etab = myDS.Tables["TblMWST"];
            foreach (DataRow Row in etab.Rows)
            {
                this.tableTblMWST.Rows.Add(Row.ItemArray); // Zeile kopieren
            }

            this.TblInvDetails.SpMwStColumn.DefaultValue = Settings.Default.setMwStDefault;
            this.AcceptChanges();
        }

        public int GetMwStIndex(decimal MwSt)
        {
            int ind = 0;
            foreach (TblMWSTRow item in TblMWST)
            {
                if (item.SpMwSt == MwSt)
                {
                    return ind;
                }
                ind++;
            }
            return 0;
        }

        public void LoadMwStTab()
        {
            string MwStTab = Settings.Default.setMwStTab;
            // MwStTab = "";
            if ((MwStTab != "") && (Settings.Default.SetVStBerechtigt == true))
            {
                this.TblMWST.Clear();
                StringReader sr = new StringReader(MwStTab);
                this.TblMWST.ReadXml(sr);
                this.TblInvDetails.SpMwStColumn.DefaultValue = Settings.Default.setMwStDefault;
            }
            else
            {
                LoadMwStDefaults();
            }

            this.TblMWST.AcceptChanges();
            this.TblInvDetails.AcceptChanges();
            ebDoc.Ribbons.ebI_Ribbon.SetRibbonButtonsEnable(true);
        }

        public string ToStringAsXml(DataTable dt)
        {
            StringWriter sw = new StringWriter();
            dt.WriteXml(sw, XmlWriteMode.IgnoreSchema);
            string s = sw.ToString();
            return s;
        }

    }
}


